
import java.util.LinkedList;

public class ProcessScheduler {
    private ProcessorManager proManager;
    private LinkedList<PCB> readyQueue;
    
    public ProcessScheduler(ProcessorManager pm) {
        proManager = pm;
        readyQueue = new LinkedList<PCB>();
    }
    public ProcessorManager getProManager(){
        return proManager;
    }
    public ProcessScheduler getPScheduler(){
      ProcessScheduler temp = this;
      return temp;
    }
    public void setProManager(ProcessorManager pm){
      proManager = pm;
    }
    public LinkedList<PCB> getReadyQueue(){
      return readyQueue;
    }
    public void setRunningStatus(Job j){
      j.getPCB().setStatus("RUNNING");
    }
    public void switchJobs(){
        int size = readyQueue.size();
        
        if(size != 0){
          Job temp = readyQueue.remove().getJob();
          if(proManager.getProcessor().getCurrJob().getPCB().getLocation() < proManager.getProcessor().getCurrJob().getPCB().getAccounting().calcTime()){
            readyQueue.add(proManager.getProcessor().getCurrJob().getPCB());
          }
          else
              jobFinished(proManager.getProcessor().getCurrJob());
          proManager.getProcessor().setCurrJob(temp);
        }
    else if(size != 0 && proManager.getProcessor().getCurrJob().getPCB().getLocation() == proManager.getProcessor().getCurrJob().getPCB().getAccounting().calcTime()){
      jobFinished(proManager.getProcessor().getCurrJob());
    }
}
    public void jobFinished(Job j){
      j.getPCB().setStatus("FINNISHED");
      j.getPCB().getAccounting().setTimeEnd(Processor.getCycleNumber());
      j.getPCB().getAccounting().calcTime();
      
      proManager.getMemManager().deallocateJob(j);
      int waitSize = proManager.getJScheduler().getWaitingQueue().size();
      if(waitSize != 0){
        proManager.bringJob(proManager.getJScheduler().getWaitingQueue().removeFirst().getJob());
      }
    }
    public void bringJob(Job j){
        proManager.bringJob(j);
}
}
